package com.fishery.monitor.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.monitor.dao.WaterDataDao;
import com.fishery.monitor.mapper.MonitorWaterDataMapper;
import com.fishery.monitor.pojo.Warning;
import com.fishery.monitor.pojo.WaterData;
import com.fishery.monitor.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class WaterDataService extends ServiceImpl<MonitorWaterDataMapper, WaterData> {

	@Autowired
	private WaterDataDao waterDataDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<WaterData> findAll() {
		return this.list(null);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public IPage<WaterData> findSearch(Map whereMap, int page, int size) {
//		Specification<WaterData> specification = createSpecification(whereMap);
//		Sort sort=new Sort(Sort.Direction.ASC,"acquisitionTime");
//		PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
//		return waterDataDao.findAll(specification, pageRequest);
		Page<WaterData> Page = new Page<>(page,size);
		QueryWrapper<WaterData> wrapper = new QueryWrapper<>();
		HashMap searchMap = new HashMap();
		searchMap.putAll(whereMap);
		searchMap.remove("checkItemName");
		searchMap.remove("typeId");
		searchMap.remove("startTime");
		searchMap.remove("endTime");
		wrapper.allEq(searchMap, false);
		return waterDataDao.findAll(Page, wrapper);
	}

	public IPage<WaterData> querySource(Map whereMap, int page, int size) {
//		Specification<WaterData> specification = createSpecification(whereMap);
//		Sort sort=new Sort(Sort.Direction.ASC,"acquisitionTime");
//		PageRequest pageRequest =  PageRequest.of(page-1, size,sort);
//		return waterDataDao.findAll(specification, pageRequest);
		Page<WaterData> Page = new Page<>(page,size);
		QueryWrapper<WaterData> wrapper = new QueryWrapper<>();
		HashMap searchMap = new HashMap();
		searchMap.putAll(whereMap);
		if (whereMap.get("startTime") != null && !"".equals(whereMap.get("startTime"))) {
			try {
				SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				wrapper.ge("acquisitionTime", dateFormat.parse((String)whereMap.get("startTime")));
				if (whereMap.get("endTime") != null && !"".equals(whereMap.get("endTime"))) {
					wrapper.le("acquisitionTime", dateFormat.parse((String)whereMap.get("endTime")));
				}
			} catch (ParseException var5) {
				var5.printStackTrace();
			}
		}
		searchMap.putAll(whereMap);
		searchMap.remove("checkItemName");
		searchMap.remove("typeId");
		searchMap.remove("startTime");
		searchMap.remove("endTime");
		wrapper.orderByDesc(new String[]{"acquisitionTime"});
		wrapper.allEq(searchMap, false);
		return waterDataDao.findAll(Page, wrapper);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<WaterData> findSearch(Map whereMap) {
		QueryWrapper<WaterData> wrapper = new QueryWrapper<>();
		HashMap searchMap = new HashMap();
		searchMap.putAll(whereMap);
		if(whereMap.get("startTime") != null && !"".equals(whereMap.get("startTime"))) {
			try {
				SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				wrapper.ge("acquisitionTime", e.parse((String)whereMap.get("startTime")));
				if(whereMap.get("endTime") != null && !"".equals(whereMap.get("endTime"))) {
					wrapper.le("acquisitionTime", e.parse((String)whereMap.get("endTime")));
				}
			} catch (ParseException var5) {
				var5.printStackTrace();
			}
		}

		searchMap.remove("checkItemName");
		searchMap.remove("typeId");
		searchMap.remove("startTime");
		searchMap.remove("endTime");
		wrapper.orderByDesc(new String[]{"acquisitionTime"});
		wrapper.allEq(searchMap, false);
		return waterDataDao.findAll(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public WaterData findById(String id) {
		return this.findById(id);
	}

	/**
	 * 增加
	 * @param waterData
	 */
	public void add(WaterData waterData) {
		waterData.setId( idWorker.nextId()+"" );
		this.save(waterData);
	}

	/**
	 * 修改
	 * @param waterData
	 */
	public void update(WaterData waterData) {
		this.save(waterData);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		this.deleteById(id);
	}



}
